package org.lingshi.shopping_manager_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.lingshi.common.entity.Category;
import org.lingshi.common.result.BaseResult;
import org.lingshi.common.service.ICategoryService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @DubboReference
    private ICategoryService categoryService;

    /**
     * 新增广告
     * @param category 广告对象
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Category category){
        categoryService.add(category);
        return BaseResult.success();
    }
    /**
     * 修改广告
     * @param category 广告对象
     * @return
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Category category){
        categoryService.update(category);
        return BaseResult.success();
    }

    /**
     * 修改广告状态
     * @param id 广告id
     * @param status 状态 0：关闭 1：启用
     * @return
     */
    @PutMapping("/updateStatus")
    public BaseResult updateStatus(Long id,Integer status){
        categoryService.updateStatus(id,status);
        return BaseResult.success();
    }

    /**
     * 根据主键删删除
     * @param ids 主键id，多条删除
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids){
        categoryService.delete(ids);
        return BaseResult.success();
    }


    /**
     * 根据id查询角色
     * @param id 主键
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Long id){
        Category role = categoryService.findById(id);
        return BaseResult.success(role);
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @return
     */
    @GetMapping("/search")
    public BaseResult search(@RequestParam(defaultValue = "1")Integer page,
                             @RequestParam(defaultValue = "10")Integer size){
        Page<Category> search = categoryService.search(page, size);
        return BaseResult.success(search);
    }
    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll(){
        return BaseResult.success(categoryService.findAll());
    }

}